//
//  WMSegmentedView.m
//  WeightMaster
//
//  Created by sungeo on 15/8/24.
//  Copyright (c) 2015年 buddysoft. All rights reserved.
//

#import "WMSegmentedView.h"
#import "BDFoundation.h"
#import <Masonry.h>
#import <EXTScope.h>

static const CGFloat Margin = 5;

@interface WMSegmentedView (){
    UIImageView * _imageButton;
    
    MASConstraint * _leading;
    MASConstraint * _trailing;
}

@end

@implementation WMSegmentedView

- (instancetype)init{
    if (self = [super init]) {
        self.layer.cornerRadius = 5;
        self.clipsToBounds = YES;
        self.backgroundColor = RGB(226, 226, 226);
        
        UIImageView * left = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"settings_button"]];
        left.layer.cornerRadius = Margin/2;
        left.clipsToBounds = YES;
        [self addSubview:left];
        _imageButton = left;
        
        UILabel * leftLabel = [[UILabel alloc] init];
        leftLabel.textAlignment = NSTextAlignmentCenter;
        leftLabel.text = @"公斤";
        [self addSubview:leftLabel];
        
        @weakify(self);
        [left mas_makeConstraints:^(MASConstraintMaker * maker){
            @strongify(self);
            _leading = maker.leading.equalTo(self.mas_left).offset(Margin);
            _trailing = maker.trailing.equalTo(self.mas_centerX).offset(-Margin);
            maker.top.equalTo(self.mas_top).offset(Margin);
            maker.bottom.equalTo(self.mas_bottom).offset(-Margin);
        }];
        
        [leftLabel mas_makeConstraints:^(MASConstraintMaker * maker){
            @strongify(self);
            _leading = maker.leading.equalTo(self.mas_left).offset(Margin);
            _trailing = maker.trailing.equalTo(self.mas_centerX).offset(-Margin);
            maker.top.equalTo(self.mas_top).offset(Margin);
            maker.bottom.equalTo(self.mas_bottom).offset(-Margin);
        }];
        
        // 右边
        UILabel * rightLabel = [[UILabel alloc] init];
        rightLabel.textAlignment = NSTextAlignmentCenter;
        rightLabel.text = @"斤";
        [self addSubview:rightLabel];
        
        [rightLabel mas_makeConstraints:^(MASConstraintMaker * maker){
            @strongify(self);
            maker.leading.equalTo(self.mas_centerX).offset(Margin);
            maker.trailing.equalTo(self.mas_right).offset(-Margin);
            maker.top.equalTo(self.mas_top).offset(Margin);
            maker.bottom.equalTo(self.mas_bottom).offset(-Margin);
        }];
        
        UITapGestureRecognizer * singleTap =  [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touched:)];
        [singleTap setNumberOfTapsRequired:1];
        [self setUserInteractionEnabled:YES];
        [self addGestureRecognizer:singleTap];
        
        _selectedIndex = 0;
    }
    
    return self;
}

- (void)touched:(id)sender{
    CGFloat time = 0.5;
    if (nil == sender) {
        time = 0;
    }
    
    if (_selectedIndex == 0) {
        _selectedIndex = 1;
        
        [UIView animateWithDuration:time animations:^{
            [_imageButton mas_remakeConstraints:^(MASConstraintMaker * maker){
                maker.leading.equalTo(self.mas_centerX).offset(Margin);
                maker.trailing.equalTo(self.mas_right).offset(-Margin);
                maker.top.equalTo(self.mas_top).offset(Margin);
                maker.bottom.equalTo(self.mas_bottom).offset(-Margin);
            }];
            
            [self setNeedsUpdateConstraints];
            [self layoutIfNeeded];
        }];
        
    }else{
        _selectedIndex = 0;
        
        [UIView animateWithDuration:time animations:^{
            [_imageButton mas_remakeConstraints:^(MASConstraintMaker * maker){
                maker.trailing.equalTo(self.mas_centerX).offset(-Margin);
                maker.leading.equalTo(self.mas_left).offset(Margin);
                maker.top.equalTo(self.mas_top).offset(Margin);
                maker.bottom.equalTo(self.mas_bottom).offset(-Margin);
            }];
            
            [self setNeedsUpdateConstraints];
            [self layoutIfNeeded];
        }];
    }
    
    if (nil != sender) {
        if (_delegate && [_delegate respondsToSelector:@selector(wmSegmentedView:changedWithNewIndex:)]) {
            [_delegate performSelector:@selector(wmSegmentedView:changedWithNewIndex:) withObject:@(_selectedIndex)];
        }
    }
}

- (void)setSelectedIndex:(NSUInteger)selectedIndex{
    if (selectedIndex == _selectedIndex) {
        return;
    }
    
    [self touched:nil];
}

+ (BOOL)requiresConstraintBasedLayout{
    return YES;
}

@end
